package com.yyyzzz.DAO.impl;

import com.yyyzzz.DAO.adminDao;
import com.yyyzzz.bean.Admin;
import sun.net.httpserver.AuthFilter;

import java.sql.*;

public class adminDaoimpl implements adminDao {
    private Connection conn;
    private Statement stmt;
    private PreparedStatement ps;
    ResultSet rs;

    @Override
    public int insteradmin(Admin admin) {

        String driverClass = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/yyyzzz?serverTimezone=UTC&characterEncoding=utf-8";
        String name = "root";
        String pwd = "123456";
        try {
            Class.forName(driverClass);
            conn = DriverManager.getConnection(url, name, pwd);
            String sql = "insert into admin(adminname,password)"+"values(?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, admin.getAdminname());
            ps.setString(2, admin.getPassword());
            int rs = ps.executeUpdate();
            conn.close();
            ps.close();
            return rs;
        }catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Admin selectadmin(Admin admin) {
    Admin admin1=null;
        String driverClass = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/yyyzzz?serverTimezone=UTC";
        String name = "root";
        String pwd = "123456";

        try {
            Class.forName(driverClass);
            conn = DriverManager.getConnection(url, name, pwd);
            String sql = "select * from admin where adminname=? and password=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, admin.getAdminname());
            ps.setString(2, admin.getPassword());
            rs = ps.executeQuery();
            if(rs.next()){
                admin1=new Admin();
                admin1.setAdminname(rs.getString("adminname"));
                admin1.setPassword(rs.getString("password"));
            }
            conn.close();
            ps.close();
            return admin1;
        }catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
